package servlet.admin;

import dao.UserDao;
import dao.service.UserDaoImp;
import modle.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

/**
 * @author cloudJiang
 */

@WebServlet("/manage/adminDoUserGet")
public class DoUserGet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //page ： 页数  count ： 每一页显示的用户数量
        int page = 1;
        int count = 5;
        int totalPage = 0;
        int numOfUser = 0;
        String searchWord = null;

        //获取用户记录
        UserDao userDao = new UserDaoImp();
        //获取用户需要跳转的页面
        String linkPage = req.getParameter("cp");
        //获取用户搜索关键字
        String keywords = req.getParameter("keywords");

        if (linkPage != null) {
            page = Integer.parseInt(linkPage);
        }

        if (keywords != null) {
            searchWord = keywords;

            ArrayList<User> users = userDao.getUserList(searchWord, page, count);
            numOfUser = userDao.getNumOfUsers(searchWord);

            if ((numOfUser % count != 0)) {
                totalPage = numOfUser / count + 1;
            } else {
                totalPage = numOfUser / count;
            }

            //将数据添加到域
            req.setAttribute("userList", users);
            req.setAttribute("page", page);
            req.setAttribute("totalPage", totalPage);
            req.setAttribute("numOfUser", numOfUser);
            req.setAttribute("searchKey", "&keywords=" + keywords);

        } else {
            ArrayList<User> users = userDao.getUserList(page, count);
            numOfUser = userDao.getNumOfUsers();

            if ((numOfUser % count != 0)) {
                totalPage = numOfUser / count + 1;
            } else {
                totalPage = numOfUser / count;
            }

            //将数据添加到域
            req.setAttribute("userList", users);
            req.setAttribute("page", page);
            req.setAttribute("totalPage", totalPage);
            req.setAttribute("numOfUser", numOfUser);

        }
        //将数据发送到jsp文件里
        req.getRequestDispatcher("adminUser.jsp").forward(req, resp);
    }

   /*     @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符集
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        String keywords = req.getParameter("keywords");
        //page ： 页数  count ： 每一页显示的用户数量
        int page = 1;
        int count = 5;
        int totalPage = 0;
        int numOfUser = 0;

        //获取用户需要跳转的页面
        String linkPage = req.getParameter("cp");

        if (linkPage != null) {
            page = Integer.parseInt(linkPage);
        }

        //获取用户记录
        UserDao userDao = new UserDaoImp();
        ArrayList<User> users = userDao.getUserList(keywords, page, count);
        numOfUser = userDao.getNumOfUsers(keywords);

        if ((numOfUser % count != 0)) {
            totalPage = numOfUser / count + 1;
        } else {
            totalPage = numOfUser / count;
        }

        //将数据添加到域
        if(keywords != null){
            req.setAttribute("searchKey", "&keywords=" + keywords);
        }
        req.setAttribute("userList", users);
        req.setAttribute("page", page);
        req.setAttribute("totalPage", totalPage);
        req.setAttribute("numOfUser", numOfUser);
        //将数据发送到jsp文件里
        req.getRequestDispatcher("adminUser.jsp").forward(req, resp);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //page ： 页数  count ： 每一页显示的用户数量
        int page = 1;
        int count = 5;
        int totalPage = 0;
        int numOfUser = 0;

        //获取用户需要跳转的页面
        String linkPage = req.getParameter("cp");

        if (linkPage != null) {
            page = Integer.parseInt(linkPage);
        }

        //获取用户记录
        UserDao userDao = new UserDaoImp();
        ArrayList<User> users = userDao.getUserList(page, count);
        numOfUser = userDao.getNumOfUsers();

        if ((numOfUser % count != 0)) {
            totalPage = numOfUser / count + 1;
        } else {
            totalPage = numOfUser / count;
        }

        //将数据添加到域
        req.setAttribute("userList", users);
        req.setAttribute("page", page);
        req.setAttribute("totalPage", totalPage);
        req.setAttribute("numOfUser", numOfUser);
        //将数据发送到jsp文件里
        req.getRequestDispatcher("adminUser.jsp").forward(req, resp);
    }*/
}
